<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: install.php 2010-08-31 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Page_Installer extends Engine_Package_Installer_Module
{
  function onPreInstall()
  {
	// -----------------------------------------------------------------------------------------
	
    $db = $this->getDb();
    $select = new Zend_Db_Select($db);

    $select
      ->from('engine4_core_modules')
      ->where('name = ?', 'hecore')
      ->where('enabled', 1)
      ->limit(1);

    $module_exists = $select->query()->rowCount();

    if (!$module_exists) {
      return $this->_error('Error! This plugin requires Hire-Experts Core module. It is free module and can be downloaded from Hire-Experts.com');
    }

	if (!$this->checkModule('like')) {
      return $this->_error('You should first install Like Module.');
    }
	
	// -----------------------------------------------------------------------------------------
	
	$select = new Zend_Db_Select($db);
	
	$db->insert("engine4_hecore_modules", array("name" => "pages", "version" => "4.0.2", "key" => "xxxxxxxxxxxxxxx", "installed" => 1, ));

    $select
      ->from('engine4_hecore_modules')
      ->where('name = ?', 'pages', 'STRING')
      ->where('installed = ?', 1, 'INTEGER')
      ->limit(1);

    $module_installed = $select->query()->rowCount();

    if (!$module_installed){
      $urlParams = array(
        'module' => 'hecore',
        'controller' => 'module',
        'action' => 'license',
        'name' => 'pages',
        'version' => '4.0.2',
        'format' => 'smoothbox'
      );

      $front = Zend_Controller_Front::getInstance();
      $register_url = str_replace('install/', '', $front->getRouter()->assemble($urlParams, 'default', true));

      $error_message = Zend_Registry::get('Zend_Translate')
        ->_('It is paid plugin from Hire-Experts LLC. You need to type License Key to install this module - <a class="smoothbox" href="%s">Click Here</a>');
      
      $error_message = sprintf($error_message, $register_url);

      return $this->_error($error_message);
    }

    parent::onPreInstall();
  }
  
	function onInstall()
	{
    parent::onInstall();
    
		$db = $this->getDb();
		$db->beginTransaction();
		
		try{
      
      $select = new Zend_Db_Select($db);

      // profile page
      $select
        ->from('engine4_core_pages')
        ->where('name = ?', 'user_profile_index')
        ->limit(1);
      
      $page_id = $select->query()->fetchObject()->page_id;

      // page.profile-pages

      // Check if it's already been placed
      $select = new Zend_Db_Select($db);
      $select
        ->from('engine4_core_content')
        ->where('page_id = ?', $page_id)
        ->where('type = ?', 'widget')
        ->where('name = ?', 'page.profile-pages')
        ;
      $info = $select->query()->fetch();

      if( empty($info) ) {

        // container_id (will always be there)
        $select = new Zend_Db_Select($db);
        $select
          ->from('engine4_core_content')
          ->where('page_id = ?', $page_id)
          ->where('type = ?', 'container')
          ->limit(1);
        $container_id = $select->query()->fetchObject()->content_id;

        // middle_id (will always be there)
        $select = new Zend_Db_Select($db);
        $select
          ->from('engine4_core_content')
          ->where('parent_content_id = ?', $container_id)
          ->where('type = ?', 'container')
          ->where('name = ?', 'middle')
          ->limit(1);
        $middle_id = $select->query()->fetchObject()->content_id;

        // tab_id (tab container) may not always be there
        $select
          ->reset('where')
          ->where('type = ?', 'widget')
          ->where('name = ?', 'core.container-tabs')
          ->where('page_id = ?', $page_id)
          ->limit(1);
        $tab_id = $select->query()->fetchObject();

        if( $tab_id && @$tab_id->content_id ) {
            $tab_id = $tab_id->content_id;
        } else {
          $tab_id = null;
        }

        // tab on profile
        $db->insert('engine4_core_content', array(
          'page_id' => $page_id,
          'type'    => 'widget',
          'name'    => 'page.profile-pages',
          'parent_content_id' => ($tab_id ? $tab_id : $middle_id),
          'order'   => 131,
          'params'  => '{"title":"Pages","titleCount":true}',
        ));
      }

      $db->commit();
		}
		catch (Exception $e){
			$db->rollBack();
			throw $e;
		}
	}
	
  protected function checkModule($module)
  {
  	$db = $this->getDb();
    
    $select = new Zend_Db_Select($db);
    $select
      ->from('engine4_core_modules')
      ->where('name = ?', $module)     
      ->where('enabled = 1')
			->limit(1);
      
    $info = $select->query()->fetch();

    return (!empty($info));
  }
}